<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/13
 * Time: 20:57
 */

namespace app\admin\controller;


use think\Controller;

class Category extends common
{
    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new \app\common\model\Category();
    }

    //首页
    public function index()
    {
        $data = $this->db->CateData();
        //2.分配变量
        $this->assign('data',$data);
        //1
        return view();
    }

    //添加
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $res = $this->db->add($data);
            if ($res['valid']) {
                $this->success($res['msg'], 'admin/category/index');
            } else {
                $this->error($res['msg']);
            }
        }
        return view();
    }
    //添加子分类
    public  function addSon($cid){
        //2.获取所属分类数据
        $cateData = db('category')->find($cid);
        $this->assign('cateData',$cateData);
        //3添加数据
        if(request()->isPost()){
            $data = input('post.');
            $res = $this->db->add($data);
            if ($res['valid']){
                $this->success($res['msg'],'admin/category/index');
            }else{
                $this->error($res['msg']);
            }
        }
        //1获取视图
        return view();
    }
    //编辑
    public function edit(){
        //获取分类数据
        $cid=input('param.cid');
        $oldData = db('category')->find($cid);
        $this->assign('oldData',$oldData);
        $cateData = $this->db->getCname($cid);
        $this->assign('cateData',$cateData);
        //编辑数据
        $data=input('post.');
        $res = $this->db->edit($data);
        //视图
        return view();
    }
    //删除
    public function del($cid){
        $res = $this->db->del($cid);
        if ($res['valid']){
            $this->success($res['msg'],'admin/category/index');
        }else{
            $this->error($res['msg']);
        }
    }

}